package kr.ac.kaist.simplefileexplorer.connection.push;

import java.util.ArrayList;

import kr.ac.kaist.simplefileexplorer.SeeMyFileActivity;
import kr.ac.kaist.simplefileexplorer.config.ContactConfig;
import kr.ac.kaist.simplefileexplorer.entity.ShareRequestEntity;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

class PushTask extends AsyncTask<String, Void, String> {

	final static String ContactCloudClient_TAG = "ContactCloudClient";

	protected String doInBackground(String... params) {

		String res = GooglePushClient.pushMessage(params[0], params[1], params[2]);
		Log.i(ContactCloudClient_TAG, "uuid: " + params[0] + "sender: "
				+ params[1] + "receiver: " +  params[2]);

		return res;
	}

	// Called by main thread.
	protected void onPostExecute(String result) {

		Log.i(ContactCloudClient_TAG, "onPostExecute: " + result);

	}
}

public class GooglePushClient {

	final static String GooglePushClient_TAG = "GooglePushClient";

	final static String PUSH_SERVER_URL = "https://android.googleapis.com/gcm/send";

	final static int HTTP_CONNECTION_TIMEOUT = 30 * 1000;

	public static String pushMessageByAsync(String uuid, String sender, String receiver) {

		String result = "progress";

		new PushTask().execute(uuid, sender, receiver);

		return result;
	}

	/**
	 * { "data": { "UUID": "0121224", "SENDER": "01027163370" },
	 * "registration_ids": [
	 * "APA91bENEcRpVCGaH-V80d3Cdq0DQqkNdF_PZtfDambZKCSYulooAh6FbNriOHZAwQcFtRFjzFBTDyqx3SUUGKjQ0PNrGCqc3xTzpmcsdTb922xQEnt3CDtHJx9esWQo_qZxF1CTpEw-h4RsuNF_ceHh_iuwT2HRZw"
	 * ] }
	 * 
	 * @return
	 */
	public static String pushMessage(String uuid, String sender, String receiver) {
		HttpClient client;
		HttpPost post;
		client = new DefaultHttpClient();
		String regId = "";

		Log.i(GooglePushClient_TAG, "uuid: " + uuid);

		HttpConnectionParams.setConnectionTimeout(client.getParams(),
				HTTP_CONNECTION_TIMEOUT);
		post = new HttpPost(PUSH_SERVER_URL);

		try {

			JSONObject dataObject = new JSONObject();
			dataObject.put("UUID", uuid);
			dataObject.put("SENDER", sender);

			JSONArray regIdArray = new JSONArray();
			
			if (ContactConfig.getRegistrationId(receiver).isEmpty())
			{
				return "";
			}
				
			regIdArray.put(ContactConfig.getRegistrationId(receiver));

			JSONObject rootObject = new JSONObject();
			rootObject.put("data", dataObject);
			rootObject.put("registration_ids", regIdArray);

			String requestData = rootObject.toString();
			Log.i(GooglePushClient_TAG, "json value: " + requestData);

			StringEntity jsonEntity = new StringEntity(requestData, HTTP.UTF_8);

			jsonEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json"));

			post.setHeader("Authorization", ContactConfig.GOOGLE_API_KEY);

			post.setEntity(jsonEntity);

			HttpResponse response = client.execute(post);
			HttpEntity responseEntity = response.getEntity();
			uuid = EntityUtils.toString(responseEntity);

			Log.i(GooglePushClient_TAG, "==> uuid: " + uuid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return uuid;
	}

}
